<?php
namespace Admin\Controller;
use Think\Controller;
class AdminsController extends CommonController{
    public function add() {
        if (I('post.id') == "" && I('post.username') == "") {
            $this->display();
        }
        else {
            $MODEL = M("admin");
            $where['username'] = I('post.username');
            $admin=$MODEL -> where($where)->find();
            if ($admin) {
                $this->error('用户名已注册，请重新输入');
            }
            if (I('post.password') == I('post.password1')) {
                // code...
                $MODEL->create();
                $MODEL->password=md5(I('post.password'));

                $temp = $MODEL->add();

                if ($temp) {
                    $this->success('数据添加成功！', 'Admin_list');
                }
                else {
                    $this->error('数据未插入');
                }
            }
            else {
                $this->error('两次密码不一样');
            }
        }
    }

    public function admin_list() {
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M("admin");
        $where['del'] = '0';
        $data = $MODEL->page($page_no, $page_size)->order('id desc')->where($where)->select();
        $count = $MODEL->where($where)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
    }

    //删除
    public function del() {
        $id = $_GET['id'];
        if ($id==1) {
            $this->error("超级管理员不能被删除");
        }
        $MODEL = M('admin');
        $del['del']='1';
        $MODEL->where('id='.$id)->save($del);
        $this->success('删除成功', 'Admin_list');
    }

    public function updata() {
        $id = $_GET['id'];
        $MODEL = M("admin");
        $data = $MODEL->find($id);
        $this->assign("data", $data);
        $this->display();
    } 

    public function do_updata() {

        $MODEL = M('admin');

        if (I('post.password') == I('post.password1')) {

            $MODEL->create();
            $MODEL->password=md5(I('post.password'));
            $temp = $MODEL->save();

            if ($temp) {
                $this->success('密码修改成功！', 'Admin_list');
            }
            else {
                $this->error('数据未插入');
            }
        }
        else {
            $this->error('两次密码不一样，请重新输入！');
        }
    }
}
